<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/2
 * Time: 9:43
 */

namespace app\api\controller;


use think\Db;

class Task extends Base
{
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $user_id = input('user_id');
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 任务大厅列表
     * @return \think\response\Json
     * page    页码
     */
    public function taskList(){
        $page = input('page')?input('page'):1;
        $user_id = input('user_id');
        $task = Db::table('up_task')
            ->join('up_u_user', 'task_user_id = user_id', 'left')
            ->field('task_id, task_content, task_type, task_gold, task_join_num, task_over_num, user_username, 
            user_image, task_user_id')
            ->order('task_time desc')
            ->page($page, 10)
            ->select();
        // 今日任务
        $today_task = count($this->myTask($user_id, 1));
        // 所有任务总金币数
        $data = $this->myTask($user_id, 2);
        $gold = 0;
        if($data){
            foreach($data as $k => $v){
                $gold += $v['mt_gold'];
            }
        }
        if($task){
            foreach($task as $ke => $vo){
                $is_done = Db::table('up_my_task')
                    ->where('mt_user_id', $user_id)
                    ->where('mt_task_id', $vo['task_id'])
                    ->find();
                if($is_done){
                    $task[$ke]['is_done'] = 1; // 已完成
                }else{
                    $task[$ke]['is_done'] = 2; // 未完成
                }
            }
            return json(['status' => 200, 'msg' => $task, 'today_task' => $today_task, 'total_gold' => $gold]);
        }else{
            return json(['status' => 200, 'msg' => [], 'today_task' => $today_task, 'total_gold' => $gold]);
        }
    }
    
    /**
     * 提交任务
     * @return \think\response\Json
     * type      1 列表    2 提交
     */
    public function doTask(){
        $task_id = input('task_id');
        $task_content = input('task_content');
        $user_id = input('user_id');
        $type = input('type');
        if($type == 1){
            //每天每个人只能领取三个任务
            $start_time = date('Y-m-d 00:00:01',time());
            $end_time = date('Y-m-d 23:59:59',time());
            $map['mt_time'] = ['between', [$start_time, $end_time]];
            $map['mt_user_id'] = $user_id;
            $count = Db::table('up_my_task')->where($map)->count();
            if($count>=3){
                return json(['status' => 100, 'msg' => '每天只能领取三个任务']);
            }
            $already = Db::table('up_my_task')
                ->where('mt_user_id', $user_id)
                ->where('mt_task_id', $task_id)
                ->find();
            if($already){
                return json(['status' => 0, 'msg' => '你已经做过该任务了，去完成其他任务吧']);
            }
            return json(['status' => 200, 'msg' => true]);
        }else{
            $task_info = Db::table('up_task')
                ->field('task_gold, task_join_num, task_over_num')
                ->where('task_id', $task_id)
                ->find();
            if($task_info['task_over_num'] >= $task_info['task_join_num']){
                return json(['status' => 0, 'msg' => '任务完成人数已达上限']);
            }
    
            // 1 任务完成人数+1  2 完成者增加金币  3 插入我的任务表
            Db::startTrans();
            try{
                Db::table('up_task')->where('task_id', $task_id)->setInc('task_over_num');
                Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_balance', $task_info['task_gold']);
                //被申请人要加积分
                Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_integral', $task_info['task_gold']);
                //更新被申请人账户余额
                $insert = [
                    'mt_user_id' => $user_id,
                    'mt_task_id' => $task_id,
                    'mt_content' => $task_content,
                    'mt_gold' => $task_info['task_gold'],
                    'mt_time' => date('Y-m-d H:i:s')
                ];
                Db::table('up_my_task')->insert($insert);
                Db::commit();
                return json(['status' => 200, 'msg' => '提交成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '提交失败']);
            }
        }
    }
    
    /**
     * 我的任务信息
     * @param $user_id     用户
     * @param $range       1 今日任务   2 所有任务
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function myTask($user_id, $range){
        $where['mt_user_id'] = $user_id;
        if($range == 1){ // 今日
            $begin_time = date('Y-m-d 00:00:01');
            $end_time = date('Y-m-d 23:59:59');
            $where['mt_time'] = ['between', [$begin_time, $end_time]];
        }
        $data = Db::table('up_my_task')
            ->where($where);
            if($range == 2){
                $data->field('mt_gold');
            }
        $data = $data->select();
        return $data;
    }
}